--[[
Copyright (c) 2011 Pedro Bittencourt

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--]]
dofile("writer.lua")

local testsResults = {}
local tests = {}

function test_simpleTag()
	local a = writeTag({name = "simpleTest"})
	if a == "<simpleTest/>" then
		table.insert(testsResults,{test = "simpleTag",pass = true, msg = ""})
	else 
		table.insert(testsResults,{test = "simpleTag",pass = false, msg = "Generated wrong tag:"..a})
	end
end
table.insert(tests,test_simpleTag)

function test_stringAttribute()
	local a = writeTag({name = "attributeTest", attrs = { test_att = "test" }})
	if a == "<attributeTest test_att=\"test\"/>" then 
		table.insert(testsResults,{test = "stringAttribute",pass = true, msg = ""})
	else
		table.insert(testsResults,{test = "stringAttribute",pass = false, msg = "Generated wrong tag:"..a})
	end
end
table.insert(tests,test_stringAttribute)

function test_numericAttribute()
	local a = writeTag({name = "numericAttributeTest", attrs = { number = 1 }})
	local t = "<numericAttributeTest number=1/>"
	if a == t then 
		table.insert(testsResults,{test = "numericAttribute",pass = true, msg = ""})
	else
		table.insert(testsResults,{test = "numericAttribute",pass = false, msg = "Generated wrong tag:"..a})
	end
end
table.insert(tests,test_numericAttribute)

function test_StringSimpleContent()
	local a = writeTag({name = "contentTest", contents={"testContent"}})
	local t = "<contentTest>testContent</contentTest>"
	if a == t then 
		table.insert(testsResults,{test = "simpleStringContent",pass = true, msg = ""})
	else
		table.insert(testsResults,{test = "simpleStringContent",pass = false, msg = "Generated wrong tag:"..a})
	end
end
table.insert(tests,test_StringSimpleContent)

function test_numericSimpleContent()
	local a = writeTag({name = "contentTest", contents={1}})
	local t = "<contentTest>1</contentTest>"
	if a == t then 
		table.insert(testsResults,{test = "simpleNumericContent",pass = true, msg = ""})
	else
		table.insert(testsResults,{test = "simpleNumericContent",pass = false, msg = "Generated wrong tag:"..a})
	end
end
table.insert(tests,test_numericSimpleContent)

function test_nestedTags()
	local a = writeTag({name="parent" , contents = { {name="child"} }})
	local t = "<parent>\n <child/>\n</parent>"
	if a == t then
		table.insert(testsResults,{test = "nestedTags",pass = true, msg = ""})
	else
		table.insert(testsResults,{test = "nestedTags",pass = false, msg = "Generated wrong tag:"..a})
	end
end
table.insert(tests,test_nestedTags)

function test_exampleSimplePlacemarkKML()
	local t =
[[<kml xmlns="http://www.opengis.net/kml/2.2">
 <Placemark>
  <name>Simple placemark</name>
  <description>Attached to the ground. Intelligently places itself at the height of the underlying terrain.</description>
  <Point>
   <coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
  </Point>
 </Placemark>
</kml>]]
	local Placemark = {
		name = "Placemark", 
		contents = {
			{name="name" , 
			contents={"Simple placemark"}},
			{name="description", 
			contents={"Attached to the ground. Intelligently places itself at the height of the underlying terrain."}},
			{name="Point",
			contents = {
				{name="coordinates",
				contents={"-122.0822035425683,37.42228990140251,0"}}
				}
			}
		}
	}
	local a = writeTag({name="kml", attrs={xmlns="http://www.opengis.net/kml/2.2"},contents={Placemark}})

	if a == t then 
		table.insert(testsResults,{test = "SimplePlacemarkKML",pass = true, msg = ""})
	else
		table.insert(testsResults,{test = "SimplePlacemarkKML",pass = false, msg = "Generated wrong tag:\n"..a.."\n\n"..t})
	end
end
table.insert(tests,test_exampleSimplePlacemarkKML)

for k,v in pairs(tests) do
	pcall(v)
end

function generateResults(t) 
	local res = {pass = 0, fail = 0}
	for k,v in pairs(t) do
		if v.pass then 
			print(v.test..": Passed.")
			res.pass = res.pass + 1
		else
			print(v.test..": Failed. \n    "..v.msg)
			res.fail = res.fail + 1
		end
	end
	print("Results:\n Passed:"..tostring(res.pass).." of "..tostring(res.pass+res.fail).."("..tostring(res.pass/(res.pass+res.fail)*100).."%)\n Failed:"..tostring(res.fail).." of "..tostring(res.pass+res.fail).."("..tostring(res.fail/(res.pass+res.fail)*100).."%)")
end

function generateWikiResults(t)
	local res = {pass = 0, fail = 0}
	print("|| test || pass? ||")
	for k,v in pairs(t) do
		if v.pass then 
			print("||".. v.test.."|| <font color='green'>yes</font> ||")
			res.pass = res.pass + 1
		else
			print("||".. v.test.."|| <font color='red'>no</font>||")
			res.fail = res.fail + 1
		end
	end
	local passRate = (res.pass/(res.pass+res.fail)*100) 
	local failRate = (res.fail/(res.pass+res.fail)*100)
	print("\n Total number of tests:"..tostring(res.pass+res.fail))
	if passRate == 0 then
		print("http://chart.apis.google.com/chart?cht=p3&chs=320x100&chd=t:"..passRate..","..failRate.."&chl=Fail("..failRate.."%)&chco=00FF00,FF0000&nonsense=a.png")
	elseif failRate ==0 then
		print("http://chart.apis.google.com/chart?cht=p3&chs=320x100&chd=t:"..passRate..","..failRate.."&chl=Pass("..passRate.."%)&chco=00FF00,FF0000&nonsense=a.png")
	else
		print("http://chart.apis.google.com/chart?cht=p3&chs=320x100&chd=t:"..passRate..","..failRate.."&chl=Pass("..passRate.."%)|Fail("..failRate.."%)&chco=00FF00,FF0000&nonsense=a.png")
	end
end
generateResults(testsResults)
--generateWikiResults(testsResults)
